home *** CD-ROM | disk | FTP | other *** search
/ PC World 2007 December / PCWorld_2007-12_cd.bin / v cisle / htttrack / httrack-3.41-3.exe / {app} / src / htszlib.c < prev    next >
C/C++ Source or Header  |  2007-06-15  |  5KB  |  173 lines

  1. /* ------------------------------------------------------------ */
  2. /*
  3. HTTrack Website Copier, Offline Browser for Windows and Unix
  4. Copyright (C) Xavier Roche and other contributors
  5.  
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19.  
  20.  
  21. Important notes:
  22.  
  23. - We hereby ask people using this source NOT to use it in purpose of grabbing
  24. emails addresses, or collecting any other private information on persons.
  25. This would disgrace our work, and spoil the many hours we spent on it.
  26.  
  27.  
  28. Please visit our Website: http://www.httrack.com
  29. */
  30.  
  31.  
  32. /* ------------------------------------------------------------ */
  33. /* File: Unpacking subroutines using Jean-loup Gailly's Zlib    */
  34. /*       for http compressed data                               */
  35. /* Author: Xavier Roche                                         */
  36. /* ------------------------------------------------------------ */
  37.  
  38. /* Internal engine bytecode */
  39. #define HTS_INTERNAL_BYTECODE
  40.  
  41. /* specific definitions */
  42. #include "htsbase.h"
  43. #include "htscore.h"
  44. #include "htszlib.h"
  45.  
  46. #if HTS_USEZLIB
  47. /* zlib */
  48. /*
  49. #include <zlib.h>
  50. #include "htszlib.h"
  51. */
  52.  
  53. /*
  54.   Unpack file into a new file
  55.   Return value: size of the new file, or -1 if an error occured
  56. */
  57. int hts_zunpack(char* filename,char* newfile) {
  58.     char catbuff[CATBUFF_SIZE];
  59.   if (gz_is_available && filename && newfile) {
  60.     if (filename[0] && newfile[0]) {
  61.       gzFile gz = gzopen (filename, "rb");
  62.       if (gz) {
  63.         FILE* fpout=fopen(fconv(catbuff, newfile),"wb");
  64.         int size=0;
  65.         if (fpout) {
  66.           int nr;
  67.           do {
  68.             char BIGSTK buff[1024];
  69.             nr=gzread (gz, buff, 1024);
  70.             if (nr>0) {
  71.               size+=nr;
  72.               if (fwrite(buff,1,nr,fpout) != nr)
  73.                 nr=size=-1;
  74.             }
  75.           } while(nr>0);
  76.           fclose(fpout);
  77.         } else
  78.           size=-1;
  79.         gzclose(gz);
  80.         return (int) size;
  81.       }
  82.     }
  83.   }
  84.   return -1;
  85. }
  86.  
  87. int hts_extract_meta(const char* path) {
  88.     char catbuff[CATBUFF_SIZE];
  89.   unzFile zFile = unzOpen(fconcat(catbuff,path,"hts-cache/new.zip"));
  90.   zipFile zFileOut = zipOpen(fconcat(catbuff,path,"hts-cache/meta.zip"), 0);
  91.   if (zFile != NULL && zFileOut != NULL) {
  92.     if (unzGoToFirstFile(zFile) == Z_OK) {
  93.       zip_fileinfo fi;
  94.       unz_file_info ufi;
  95.       char BIGSTK filename[HTS_URLMAXSIZE * 4];
  96.       char BIGSTK comment[8192];
  97.       memset(comment, 0, sizeof(comment));       // for truncated reads
  98.       memset(&fi, 0, sizeof(fi));
  99.       memset(&ufi, 0, sizeof(ufi));
  100.       do  {
  101.         int readSizeHeader;
  102.         filename[0] = '\0';
  103.         comment[0] = '\0';
  104.         
  105.         if (unzOpenCurrentFile(zFile) == Z_OK) {
  106.           if (
  107.             (readSizeHeader = unzGetLocalExtrafield(zFile, comment, sizeof(comment) - 2)) > 0
  108.             &&
  109.             unzGetCurrentFileInfo(zFile, &ufi, filename, sizeof(filename) - 2, NULL, 0, NULL, 0) == Z_OK
  110.             ) 
  111.           {
  112.             comment[readSizeHeader] = '\0';
  113.             fi.dosDate = ufi.dosDate;
  114.             fi.internal_fa = ufi.internal_fa;
  115.             fi.external_fa = ufi.external_fa;
  116.             if (zipOpenNewFileInZip(zFileOut,
  117.               filename,
  118.               &fi,
  119.               NULL,
  120.               0,
  121.               NULL,
  122.               0,
  123.               NULL, /* comment */
  124.               Z_DEFLATED,
  125.               Z_DEFAULT_COMPRESSION) == Z_OK)
  126.             {
  127.               if (zipWriteInFileInZip(zFileOut, comment, (int) strlen(comment)) != Z_OK) {
  128.               }
  129.               if (zipCloseFileInZip(zFileOut) != Z_OK) {
  130.               }
  131.             }
  132.           }
  133.           unzCloseCurrentFile(zFile);
  134.         }
  135.       } while( unzGoToNextFile(zFile) == Z_OK );
  136.     }
  137.     zipClose(zFileOut, "Meta-data extracted by HTTrack/"HTTRACK_VERSION);
  138.     unzClose(zFile);
  139.     return 1;
  140.   }
  141.   return 0;
  142. }
  143.  
  144. const char* hts_get_zerror(int err) {
  145.   switch(err) {
  146.     case UNZ_OK:
  147.       return "no error";
  148.       break;
  149.     case UNZ_END_OF_LIST_OF_FILE:
  150.       return "end of list of file";
  151.       break;
  152.     case UNZ_ERRNO:
  153.       return (const char*) strerror(errno);
  154.       break;
  155.     case UNZ_PARAMERROR:
  156.       return "parameter error";
  157.       break;
  158.     case UNZ_BADZIPFILE:
  159.       return "bad zip file";
  160.       break;
  161.     case UNZ_INTERNALERROR:
  162.       return "internal error";
  163.       break;
  164.     case UNZ_CRCERROR:
  165.       return "crc error";
  166.       break;
  167.     default:
  168.       return "unknown error";
  169.       break;
  170.   }
  171. }
  172. #endif
  173.